//index.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api.js');
Page({
  data: {
    loading: false,
    inputValue: '',
    curView: "rank",
    loading: true
  },
  restInput: function(e) {
    this.setData({
      error: 0,
      inputValue: e.detail.value
    })
  },
  changeView: function(event) {
    let target = event.target;
    let dataset = event.target.dataset;
    if (dataset.view == "rank") {
      this.data.curView = 'rank';
    } else {
      this.data.curView = 'activity';
    }
    this.setData({
      curView: this.data.curView,
      inputValue: ''
    })
  },
  //搜索steam用户昵称
  searchName: function() {
    // 获取输入框中的用户昵称
    let inputValue = this.data.inputValue;
    if (inputValue.length > 0 && !inputValue.match(/^\s+$/g)) {
      // wx.redirectTo({ url: '../list/list?act=bind&name=' + inputValue })
      this.bind(inputValue);
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '输入的昵称不能为空'
      })
    }
  },

  //绑定操作
  bind: function(value) {
    let self = this;
    let numRegExp = /^[0-9]{17}$/;
    let token = wx.getStorageSync('token');
    if (numRegExp.test(value)) {
      this.bindFn(value)
    } else {
      self.setData({
        loading: false
      })
      api.http('pubg/bindByGameNickName', 'POST', {
        gameNickName: value,
        token:token
      }).then((res) => {
        if (res.data.code == 1) {
          wx.redirectTo({ url: '../index/index' });
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.message,
          })
          self.setData({
            loading: true
          })
        }
      })
    }
  },
  // 分享
  onShareAppMessage: function() {
    return {
      title: '绝地求生群排行',
      path: 'pages/index/index'
    }
  }
})